package com.ea.eadp.nexus.test.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * User: woodywu
 */
public class SystemProperties {

    public static final String IdentityPortalUri = "identity.client.uri";
    public static final String NexusConnectUri = "nexus.connect.uri";
    public static final String NexusPortalUri = "nexus.portal.uri";
    public static final String NexusHttpsConnectUri = "nexus.httpsconnect.uri";
    public static final String NexusHttpsProxyUri = "nexus.httpsproxy.uri";
    public static final String NexusProxytUri = "nexus.proxy.uri";
    public static final String NexusHttpConnectInternalUri = "nexus.connect.internal.uri";
    public static final String NexusHttpProxyInternalUri = "nexus.proxy.internal.uri";
    public static final String ScreenshotPath="screenshotPath";
    public static final String ClientId = "connect.client_id";
    public static final String RedirectUri = "connect.redirect_uri";
    public static final String Serect = "connect.secret";
    public static final String HttpsClientPort = "httpsClient.port";
    public static final String LockboxUri="lockbox.uri";
    public static final String AcutalUri="actualRedirect.uri";
    public static final String DefaultSignupCountry="country.default";
    public static final String NucleusUserIP="nuclues.user.ip";
    public static final String NucleusRequestorID="nuclues.requestorid";
    public static final String ChromeDriver="webdriver.chrome.driver";
    public static final String ChromeDriverLinux="webdriver.chrome.driver.linux";
    public static final String RunAppClientReload = "test.appclient.reload";
    public static final String AppClientReloadHost = "test.onebox.host";
    public static final String AppClientReloadPath = "test.appclient.path";
    public static final String AppClientUniverse = "appclient.universe";
    public static final String FacebookProxyHost = "facebook.proxy.host";
    public static final String FacebookProxyPort = "facebook.proxy.port";
    public static final String FosServerSecret = "fosserver.secret";
    public static final String MongoReplicationSet = "mongo.replicationSet";
    public static final String socketTimeout = "http.client.socket.timeout";
    public static final String connectionTimeout = "http.client.connection.timeout";
    public static final String connectionTimeToLive = "http.connection.timeToLive";
    public static final String httpMaxTotal = "http.max.total";
    public static final String httpMaxPerRoute = "http.max.perroute";
    public static final String twofactorCodeCooldownTime = "twofactorcode.cooldowntime";

    private static Log log = LogFactory.getLog(SystemProperties.class);
    private static SystemProperties instance;

    private SystemProperties() {
    }

    public static synchronized SystemProperties instance() {
        if (instance == null) {
            instance = new SystemProperties();
            init();
        }
        return instance;
    }

    /**
     * Gets a property value from key propertyName.  Throws IllegalArgumentException if property not found
     *
     * @param propertyName
     * @return
     */
    public String getProperty(String propertyName) {
        String prop = System.getProperty(propertyName);
        if (prop == null) {
            throw new IllegalArgumentException("No system property for key " + propertyName);
        }
        return prop;
    }

    /**
     * Gets a property with key propertyName.  Returns defaultValue if no property found.
     *
     * @param propertyName
     * @param defaultValue
     * @return
     */
    public String getProperty(String propertyName, String defaultValue) {
        return System.getProperty(propertyName, defaultValue);
    }

    private static void init() {
        String universe = System.getProperty("test.universe");

        if (universe != null && !universe.startsWith("${"))
            loadTestProperties(String.format("config/%s.properties", universe));
        loadTestProperties("config/default.properties");
    }

    private static void loadTestProperties(String path) {
        Properties props = new Properties();
        try {
            InputStream io = instance.getClass().getClassLoader().getResourceAsStream(path);
            if (io != null)
                props.load(io);
            else
                throw new RuntimeException("Could not find the '" + path + "' properties file in the 'config' resource directory, as specified in test.universe.");
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }

        log.info("*** Setting system properties from " + path + ":");
        for (Object key : props.keySet()) {
            if (!System.getProperties().containsKey(key) ||
                    System.getProperty((String) key).startsWith("${")) {
                System.setProperty((String) key, props.getProperty((String) key));
                log.info(key + ": " + System.getProperty((String) key));
            } else {
                log.info("ALREADY SET - " + key + ": " + System.getProperty((String) key));
            }
        }
        log.info("*** Finished loading " + path);
    }
}
